package com.governtalk.backend.controller;

import com.governtalk.backend.dto.group.StudyGroupDto;
import com.governtalk.backend.service.StudyGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/groups")
public class StudyGroupController {
    
    @Autowired
    private StudyGroupService studyGroupService;
    
    @GetMapping
    public ResponseEntity<Page<StudyGroupDto>> getAllGroups(Pageable pageable) {
        Page<StudyGroupDto> groups = studyGroupService.getAllGroups(pageable);
        return ResponseEntity.ok(groups);
    }
    
    @GetMapping("/category/{categoryId}")
    public ResponseEntity<Page<StudyGroupDto>> getGroupsByCategory(
            @PathVariable Long categoryId, 
            Pageable pageable) {
        Page<StudyGroupDto> groups = studyGroupService.getGroupsByCategory(categoryId, pageable);
        return ResponseEntity.ok(groups);
    }
    
    @GetMapping("/search")
    public ResponseEntity<Page<StudyGroupDto>> searchGroups(
            @RequestParam String keyword, 
            Pageable pageable) {
        Page<StudyGroupDto> groups = studyGroupService.searchGroups(keyword, pageable);
        return ResponseEntity.ok(groups);
    }
    
    @GetMapping("/popular")
    public ResponseEntity<Page<StudyGroupDto>> getPopularGroups(Pageable pageable) {
        Page<StudyGroupDto> groups = studyGroupService.getPopularGroups(pageable);
        return ResponseEntity.ok(groups);
    }
    
    @GetMapping("/latest")
    public ResponseEntity<Page<StudyGroupDto>> getLatestGroups(Pageable pageable) {
        Page<StudyGroupDto> groups = studyGroupService.getLatestGroups(pageable);
        return ResponseEntity.ok(groups);
    }
    
    @GetMapping("/{groupId}")
    public ResponseEntity<StudyGroupDto> getGroupById(@PathVariable Long groupId) {
        StudyGroupDto group = studyGroupService.getGroupById(groupId);
        return ResponseEntity.ok(group);
    }
    
    @PostMapping
    public ResponseEntity<StudyGroupDto> createGroup(@RequestBody StudyGroupDto groupDto) {
        StudyGroupDto createdGroup = studyGroupService.createGroup(groupDto);
        return ResponseEntity.ok(createdGroup);
    }
    
    @PutMapping("/{groupId}")
    public ResponseEntity<StudyGroupDto> updateGroup(
            @PathVariable Long groupId, 
            @RequestBody StudyGroupDto groupDto) {
        StudyGroupDto updatedGroup = studyGroupService.updateGroup(groupId, groupDto);
        return ResponseEntity.ok(updatedGroup);
    }
    
    @DeleteMapping("/{groupId}")
    public ResponseEntity<Void> deleteGroup(@PathVariable Long groupId) {
        studyGroupService.deleteGroup(groupId);
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/{groupId}/join")
    public ResponseEntity<Void> joinGroup(@PathVariable Long groupId) {
        studyGroupService.joinGroup(groupId);
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/{groupId}/leave")
    public ResponseEntity<Void> leaveGroup(@PathVariable Long groupId) {
        studyGroupService.leaveGroup(groupId);
        return ResponseEntity.ok().build();
    }
    
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<StudyGroupDto>> getUserGroups(@PathVariable Long userId) {
        List<StudyGroupDto> groups = studyGroupService.getUserGroups(userId);
        return ResponseEntity.ok(groups);
    }
    
    @GetMapping("/{groupId}/joined")
    public ResponseEntity<Boolean> isUserJoined(@PathVariable Long groupId) {
        // 这里需要从当前用户上下文获取用户ID
        // 暂时返回false，实际实现中需要获取当前登录用户ID
        boolean isJoined = studyGroupService.isUserJoined(groupId, 1L); // 临时使用用户ID 1
        return ResponseEntity.ok(isJoined);
    }
}

